package cn.tedu.charging.device.service.impl;

import cn.tedu.charging.commen.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.commen.pojo.vo.ChargingStationVO;
import cn.tedu.charging.commen.pojo.vo.StationVO;
import cn.tedu.charging.device.dao.mapper.DeviceMapper;
import cn.tedu.charging.device.dao.mapper.UpdateGunMapper;
import cn.tedu.charging.device.dao.repository.ChargingStationCacheRepository;
import cn.tedu.charging.device.pojo.po.ChargingGunInfoPO;
import cn.tedu.charging.device.pojo.po.ChargingStationPO;
import cn.tedu.charging.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
@Slf4j
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private UpdateGunMapper updateGunMapper;

    @Autowired
    @Qualifier("GEOANDNoShareHash")
    private ChargingStationCacheRepository chargingStationCacheRepository;
    @Override
    public StationVO getStationInfo(Integer gunId) {
        log.debug("获取场站信息业务，参数={}",gunId);
        StationVO stationVO = deviceMapper.getStationInfoByGunId(gunId);
        log.debug("获取场站信息业务，结果={}",stationVO);
        return stationVO;
    }

    @Override
    public Boolean updateGunStatus(GunStatusUpdateParam gunStatusUpdateParam) {
        log.debug("更新枪状态业务，参数={}",gunStatusUpdateParam);
        ChargingGunInfoPO chargingGunInfoPO = new ChargingGunInfoPO();
        chargingGunInfoPO.setGunStatus(gunStatusUpdateParam.getStatus());
        chargingGunInfoPO.setId(gunStatusUpdateParam.getGunId());
        Integer row = updateGunMapper.updateById(chargingGunInfoPO);
        if (row == 0){
            return false;
        }
        return true;
    }

    @Override
    public List<ChargingStationVO> getNearStations(Double longitude, Double latitude, Double distance) {
        log.debug("获取附近场站业务，参数:经度:{},纬度:{},半径:{}",longitude,latitude,distance);
        List<ChargingStationPO> stationPOS = chargingStationCacheRepository.getStationsFromRedis(longitude, latitude, distance);
        List<ChargingStationVO> stationVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(stationPOS)){
            for (ChargingStationPO stationPO : stationPOS) {
                ChargingStationVO stationVO = new ChargingStationVO();
                BeanUtils.copyProperties(stationPO,stationVO);
                stationVOS.add(stationVO);
            }
            return stationVOS;
        }
        return Collections.emptyList();
    }


}
